package com.lbs.jiepiaisi.biz;

import android.content.Intent;
import android.util.Log;

import com.lbs.jiepiaisi.bean.EquipmentEntity;
import com.lbs.jiepiaisi.bean.EquipmentEqNoEntity;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.manager.SharePreferenceManager;
import com.lbs.jiepiaisi.parser.SelCarsAccountParser;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.HttpUtil;
import com.lbs.jiepiaisi.utils.UrlUtil;

import okhttp3.FormBody;
import okhttp3.Response;

/**
 *
 * 时间：2017/8/8 10:06
 * 作用：加载车业务类
 */

public class SelCarsBiz {
    //加载车
    public void selCarsBiz(final String str, final String page, final String size, final boolean isReload, final String receiverFlag){
        //获得sp存储的部门id
        SharePreferenceManager mSharePreference=SharePreferenceManager.getInstance(App.instance.getApplicationContext());
        int deptId=mSharePreference.getInt(Constant.ACCOUNT_DEPTID,Constant.SP_NULL);
        FormBody formBody = new FormBody
                .Builder()
                .add("deptId",deptId + "")//设置参数名称和参数值/*String.valueOf(deptId)*/
                .add("str",str)//检索值
                .add("page",page)//页码
                .add("size",size)//记录数
                .add("status","")//设备状态
                .add("actStatus","")//激活状态
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_SELCARS_URL, formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {

                EquipmentEntity equipmentEntity=new EquipmentEntity();
                try{
                    //返回结果
                    String jsonString = response.body().string();
                    equipmentEntity=SelCarsAccountParser.parser(jsonString);
                    equipmentEntity.setPage(Integer.parseInt(page));
                    Log.e("TAG","Page,Size:"+page+","+size);
                    Log.e("TAG","Size："+equipmentEntity.getResult().getItems().size());
                    Log.e("TAG","查询车辆信息："+jsonString);
                    if(equipmentEntity.getResult().getItems().size() == 1000){
                        selCarsBiz(str,(Integer.parseInt(page)+1)+"",size,isReload,receiverFlag);
                    }
                }catch (Exception e){
                    equipmentEntity.setCode(Constant.STATUS_NETWORK_ERROR);
                    e.printStackTrace();
                    Intent intent = new Intent(receiverFlag);
                    intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                    intent.putExtra(Constant.KEY_DATA2, isReload);
                    App.instance.sendBroadcast(intent);
                }finally {
                    Intent intent = new Intent(receiverFlag);
                    intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                    intent.putExtra(Constant.KEY_DATA2, isReload);
                    App.instance.sendBroadcast(intent);
                }
            }
            @Override
            public void failed() {
                EquipmentEntity equipmentEntity=new EquipmentEntity();
                equipmentEntity.setCode(Constant.STATUS_NO_NET);
                Intent intent = new Intent(receiverFlag);
                intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                App.instance.sendBroadcast(intent);
            }
            @Override
            public void noNetwork() {
                EquipmentEntity equipmentEntity=new EquipmentEntity();
                equipmentEntity.setCode(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(receiverFlag);
                intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                App.instance.sendBroadcast(intent);
            }
        });
    }


    public void selCarsBizAccountInfo(final String str, final String page, final String size, String deptId, final boolean isReload, final String receiverFlag){
        //获得sp存储的部门id
        SharePreferenceManager mSharePreference=SharePreferenceManager.getInstance(App.instance.getApplicationContext());
        //int deptId=mSharePreference.getInt(Constant.ACCOUNT_DEPTID,Constant.SP_NULL);
        FormBody formBody = new FormBody
                .Builder()
                .add("deptId",deptId + "")//设置参数名称和参数值/*String.valueOf(deptId)*/
                .add("str",str)//检索值
                .add("page",page)//页码
                .add("size",size)//记录数
                .add("status","")//设备状态
                .add("actStatus","")//激活状态
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_SELCARS_URL, formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {

                EquipmentEntity equipmentEntity=new EquipmentEntity();
                try{
                    //返回结果
                    String jsonString = response.body().string();
                    equipmentEntity=SelCarsAccountParser.parser(jsonString);
                    equipmentEntity.setPage(Integer.parseInt(page));
                    Log.e("TAG","Page,Size:"+page+","+size);
                    Log.e("TAG","Size："+equipmentEntity.getResult().getItems().size());
                    Log.e("TAG","查询车辆信息："+jsonString);
                    if(equipmentEntity.getResult().getItems().size() == 1000){
                        selCarsBiz(str,(Integer.parseInt(page)+1)+"",size,isReload,receiverFlag);
                    }
                }catch (Exception e){
                    equipmentEntity.setCode(Constant.STATUS_NETWORK_ERROR);
                    e.printStackTrace();
                    Intent intent = new Intent(receiverFlag);
                    intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                    intent.putExtra(Constant.KEY_DATA2, isReload);
                    App.instance.sendBroadcast(intent);
                }finally {
                    Intent intent = new Intent(receiverFlag);
                    intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                    intent.putExtra(Constant.KEY_DATA2, isReload);
                    App.instance.sendBroadcast(intent);
                }
            }
            @Override
            public void failed() {
                EquipmentEntity equipmentEntity=new EquipmentEntity();
                equipmentEntity.setCode(Constant.STATUS_NO_NET);
                Intent intent = new Intent(receiverFlag);
                intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                App.instance.sendBroadcast(intent);
            }
            @Override
            public void noNetwork() {
                EquipmentEntity equipmentEntity=new EquipmentEntity();
                equipmentEntity.setCode(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(receiverFlag);
                intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                App.instance.sendBroadcast(intent);
            }
        });
    }


    //加载车
    public void selCarsByEqNumerBiz(final String eqNo){
        //获得sp存储的部门id
        FormBody formBody = new FormBody
                .Builder()
                .add("eqNo",eqNo)//设置参数名称和参数值/*String.valueOf(deptId)*/
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_VIN_SELCARS_URL, formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {

                EquipmentEqNoEntity  equipmentEntity= new EquipmentEqNoEntity();
                try{
                    //返回结果
                    String jsonString = response.body().string();
                    equipmentEntity=SelCarsAccountParser.parserEq(jsonString);

                }catch (Exception e){
                    equipmentEntity.setStatus(Constant.STATUS_NETWORK_ERROR);
                    e.printStackTrace();
                    Intent intent = new Intent(Constant.ACCOUNT_CAR_SELCARS);
                    intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                    App.instance.sendBroadcast(intent);
                }finally {
                    Intent intent = new Intent(Constant.ACCOUNT_CAR_SELCARS);
                    intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                    App.instance.sendBroadcast(intent);
                }
            }
            @Override
            public void failed() {
                EquipmentEqNoEntity equipmentEntity=new EquipmentEqNoEntity();
                equipmentEntity.setStatus(Constant.STATUS_NO_NET);
                Intent intent = new Intent(Constant.ACCOUNT_CAR_SELCARS);
                intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                App.instance.sendBroadcast(intent);
            }
            @Override
            public void noNetwork() {
                EquipmentEqNoEntity equipmentEntity=new EquipmentEqNoEntity();
                equipmentEntity.setStatus(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(Constant.ACCOUNT_CAR_SELCARS);
                intent.putExtra(Constant.KEY_DATA, equipmentEntity);
                App.instance.sendBroadcast(intent);
            }
        });
    }


}
